//
//  MKTChatItemsView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/9.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatItemsView.h"
#import "MKTChatItemTVCellBase.h"
@implementation MKTChatItemsView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.contentInsets = UIEdgeInsetsMake(10, 0, 10, 0);
        self.tableView = [[MKUITableView alloc] initWithFrame:self.frame style:(UITableViewStylePlain)];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.scrollEnabled = NO;
        [self.tableView mk_hiddenFooterAreaSeparators];
        [self addSubview:self.tableView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGRect f1 = UIEdgeInsetsInsetRect(bounds, self.contentInsets);
    BOOL sizechange = !CGSizeEqualToSize(f1.size, self.tableView.frame.size);
    self.tableView.frame = f1;
    if(sizechange){
        [self reloadData];
    }
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = size;
    s.width = size.width;
    UIEdgeInsets insets = self.contentInsets;
    size.width -= insets.left+insets.right;
    s.height = [self.tableView mk_heightThatFits:size.width];
    s.height += insets.top+insets.bottom;
    return s;
}
- (void)reloadData{
    [self.tableView.model removeAllSectionModels];
    for (MKTChatItemBase *item in self.itemslist) {
        MKUITableViewCellModel *cm = [[MKUITableViewCellModel alloc] init];
        cm.modelValue = item;
        cm.cellClass = [item tableViewCellClass];
        [self.tableView.model addCellModel:cm];
    }
    [self.tableView.model reloadTableViewData];
}
@end
